package cc.lj.internal.coupons.domain.ticket.service.impl;

import cc.lj.internal.core.model.PageBean;
import cc.lj.internal.core.model.PageParam;
import cc.lj.internal.coupons.domain.ticket.model.vo.TicketPackageDO;
import cc.lj.internal.coupons.domain.ticket.model.vo.convert.TicketPackagePOConvertDO;
import cc.lj.internal.coupons.domain.ticket.repository.ITicketPackageRepository;
import cc.lj.internal.coupons.domain.ticket.service.ITicketPackageDomainService;
import cc.lj.internal.coupons.infrastructure.po.TicketPackagePO;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 券包表领域服务service实现类
 * @author liangxifeng
 * @date 2023-08-24
 */
@Service("ticketPackageDomainServiceImpl")
@Slf4j
public class TicketPackageDomainServiceImpl implements ITicketPackageDomainService {
    //电子券批次表仓储接口
    @Resource(name = "ticketPackageRepository")
    private ITicketPackageRepository repository;
    


    /**
     * 分页查询
     * @param pageParam
     * @return
     */
    @Override
    public PageBean<TicketPackageDO> selectPage(PageParam<TicketPackageDO> pageParam) {
        PageInfo<TicketPackagePO> pageInfo = repository.selectPage(pageConvert(pageParam));

        List<TicketPackageDO> list = new ArrayList();
        if(pageInfo.getList().size() > 0)
        {
            for (TicketPackagePO TicketPackagePO : pageInfo.getList()) {
                TicketPackageDO activityDO = TicketPackagePOConvertDO.INSTANCE.convert(TicketPackagePO);
                list.add(activityDO);
            }
        }
        //list的po转换为do后再组装PageBean将其返回上一层
        PageBean pageBean = PageBean.createPageBean(pageInfo.getPageNum(),
                pageInfo.getPageSize(), pageInfo.getTotal(), list);
        return pageBean;
    }


    /**
     * 分页DO to PO
     * @param pageParam
     * @return
     */
    public PageParam<TicketPackagePO> pageConvert(PageParam<TicketPackageDO> pageParam) {
        PageParam<TicketPackagePO> pageParamR = new PageParam<TicketPackagePO>();
        pageParamR.setPageNum( pageParam.getPageNum() ) ;
        pageParamR.setPageSize( pageParam.getPageSize() );
        pageParamR.setOrderBy( pageParam.getOrderBy() );
        TicketPackagePO activityPO = TicketPackagePOConvertDO.INSTANCE.convert(pageParam.getParam());
        pageParamR.setParam(activityPO);
        return pageParamR;
    }

    /**
     * 通过主键查询一条记录
     * @param id
     * @return
     */
    public TicketPackageDO getById(Integer id)
    {
        TicketPackagePO TicketPackagePO = repository.getById(id);
        return TicketPackagePOConvertDO.INSTANCE.convert(TicketPackagePO);
    }

    /**
     * 根据id修改
     * @param TicketPackageDO
     * @return
     */
    public Integer updateById(TicketPackageDO TicketPackageDO) {
        //TicketPackagePO mainPO = repository.getById(TicketPackageDO.getId());
        TicketPackagePO TicketPackagePO = TicketPackagePOConvertDO.INSTANCE.convert(TicketPackageDO);
        return repository.updateById(TicketPackagePO);
    }

    /**
     * 标准批量获取数据的方法
     * @param TicketPackageDO
     * @return
     */
    public List<TicketPackageDO> getList(TicketPackageDO TicketPackageDO)
    {
        TicketPackagePO TicketPackagePO = TicketPackagePOConvertDO.INSTANCE.convert(TicketPackageDO);
        List<TicketPackagePO> list = repository.getList(TicketPackagePO);
        List<TicketPackageDO> newList = new ArrayList<>();
        for(TicketPackagePO item : list)
        {
            newList.add(TicketPackagePOConvertDO.INSTANCE.convert(item));
        }
        return newList;
    }

}
